package com.kmproject.haifu.mvp.presenter;

import android.app.Activity;
import android.app.Application;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.App;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.widget.CustomPopupWindow;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.hfinterface.HouseReasonRemovalCallBack;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.MoreHousePublishRentContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowInfoBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HouseParmarBean;
import com.kmproject.haifu.mvp.ui.adapter.PopuwindowReasonRemovalAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/17/2019 21:49
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@FragmentScope
public class MoreHousePublishRentPresenter extends BasePresenter<MoreHousePublishRentContract.Model, MoreHousePublishRentContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public MoreHousePublishRentPresenter(MoreHousePublishRentContract.Model model, MoreHousePublishRentContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 查询租房数据
     * @param houseParmarBean
     */
    public void listHouseRentCurrentUser(HouseParmarBean houseParmarBean)
    {
        mModel.listHouseRentCurrentUser(AppUtil.entityToMap(houseParmarBean))
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HouseRentListShowInfoBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HouseRentListShowInfoBean>> listResultBean) {
                     if (AppUtil.isSuccess(listResultBean))
                     {
                         mRootView.getMeListSuccess(listResultBean.getData());
                     }else {
                         mRootView.showMessage(listResultBean.getMessage());
                     }
                    }
                });
    }
    /**
     * 获取店铺选择类型
     */
    public void dictList(String value,String category) {
        mModel.dictList(value, category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.dictListSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 设置筛选条件
     * @param houseParmarBean
     * @param housePayWayBean
     * @return
     */
    public HouseParmarBean setParmat(HouseParmarBean houseParmarBean, HousePayWayBean housePayWayBean)
    {
        houseParmarBean.setPage(1);
        //被清空的时候
        houseParmarBean.setRentType(null);//类型
        houseParmarBean.setRoomCount(null);//几室
        houseParmarBean.setRentDetailList(null);//出租要求，房源特色 等筛选条件取数据字典id
        houseParmarBean.setAcreage(null);//商铺面积
        houseParmarBean.setRentRoom(null);//房屋类型
        houseParmarBean.setDailyRent(null);//商铺 短租
        houseParmarBean.setDis(null);
        if (housePayWayBean==null)
        {
            return houseParmarBean;
        }
        houseParmarBean.setRentType(housePayWayBean.getCategory());
        if ( houseParmarBean.getRentDetailList()==null)
        {
            houseParmarBean.setRentDetailList(new ArrayList<>());
        }
        if (housePayWayBean.getChildren()!=null)
        {

            for (HousePayWayBean housePayWayBean1:housePayWayBean.getChildren())
            {
                if ("roomCount".equals(housePayWayBean1.getCode()))
                {
                    //几室几厅，单选
                    for (HousePayWayBean housePayWayBean2:housePayWayBean1.getChildren())
                    {
                        if (housePayWayBean2.isSelect())
                        {
                            houseParmarBean.setRoomCount(housePayWayBean2.getDictId());//几室
                            break;
                        }
                    }
                }else if ("rentDetailList".equals(housePayWayBean1.getCode()))
                {
                    List<String> temRentDetailList=new ArrayList<>();
                    //亮点 和要求
                    for (HousePayWayBean housePayWayBean3:housePayWayBean1.getChildren())
                    {
                        if (housePayWayBean3.isSelect())
                        {
                            temRentDetailList.add(housePayWayBean3.getDictId());
                        }
                    }
                    if (!temRentDetailList.isEmpty())
                    {

                        houseParmarBean.getRentDetailList().addAll(temRentDetailList);
                    }

                }else if ("rentRoom".equals(housePayWayBean1.getCode()))
                {
                    //合租的房屋类型 独立房间
                    for (HousePayWayBean housePayWayBean4:housePayWayBean1.getChildren())
                    {
                        if (housePayWayBean4.isSelect())
                        {
                            houseParmarBean.setRentRoom(housePayWayBean4.getDictId());//独立房间
                            break;
                        }
                    }
                }else if ("acreage".equals(housePayWayBean1.getCode()))
                {
                    //商业的 面积
                    for (HousePayWayBean housePayWayBean5:housePayWayBean1.getChildren())
                    {
                        if (housePayWayBean5.isSelect())
                        {
                            houseParmarBean.setAcreage(housePayWayBean5.getDictId());//面积
                            break;
                        }
                    }
                }else if ("dailyRent".equals(housePayWayBean1.getCode()))
                {
                    //商业的 是否短租
                    for (HousePayWayBean housePayWayBean6:housePayWayBean1.getChildren())
                    {
                        if (housePayWayBean6.isSelect())
                        {
                            houseParmarBean.setDailyRent(housePayWayBean6.getDictId());//是否短租
                            break;
                        }
                    }
                }else if ("transferOrLease".equals(housePayWayBean1.getCode()))
                {
                    //商业的 转租还是商铺
                    for (HousePayWayBean housePayWayBean7:housePayWayBean1.getChildren())
                    {
                        if (housePayWayBean7.isSelect())
                        {
                            houseParmarBean.setTransferOrLease(housePayWayBean7.getDictId());
                            break;
                        }
                    }
                }

            }
        }

        return houseParmarBean;
    }
    /**
     * 获取租房筛选条件
     * @param houseCategory
     * @param rentType
     */
    public  void  getQueryConditionTree(String houseCategory, String rentType,List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans!=null&&!housePayWayBeans.isEmpty())
        {
            mRootView.getQueryConditionTreeSuccess( houseCategory,housePayWayBeans);
            return;
        }
        mModel.getQueryConditionTree(houseCategory,rentType).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean))
                        {
                            mRootView.getQueryConditionTreeSuccess(houseCategory,listResultBean.getData());
                        }else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 获取付款方式,获取字典的都从这个接口获取
     * @param value
     * @param category
     */
    public void getHousePayWayBean(String value, String category,List<HousePayWayBean> housePayWayBeans)
    {
        if (housePayWayBeans!=null&&!housePayWayBeans.isEmpty())
        {
            mRootView.getHousePayWayBeanSuccess(value,housePayWayBeans);
            return;
        }
        if (value.equals(Api.HOUSE_AREA))
        {
            mModel.getAreaTree(Api.APP_DOMAIN_ZIDIAN+"/app/baseArea/getAreaTree",value,category).compose(RxUtils.applySchedulers(mRootView))
                    .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                        @Override
                        public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                            if (AppUtil.isSuccess(listResultBean))
                            {
                                mRootView.getHousePayWayBeanSuccess(value,listResultBean.getData());
                            }else {
                                mRootView.showMessage(listResultBean.getMessage());
                            }

                        }
                    });

            return;
        }
        mModel.getHousePayWayBean(Api.APP_DOMAIN_ZIDIAN+"/app/dictList",value,category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean))
                        {
                            mRootView.getHousePayWayBeanSuccess(value,listResultBean.getData());
                        }else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }

                    }
                });
    }





    /**
     * 下架提示
     */
    public  void houseReasonRemoval(Activity activity, View mParentView, List<HousePayWayBean> housePayWayBeans, HouseReasonRemovalCallBack houseReasonRemovalCallBack) {
        CustomPopupWindow customPopupWindow = new CustomPopupWindow.Builder().parentView(mParentView).contentView(CustomPopupWindow.inflateView(activity, R.layout.popuwindow_reason_removal))
                .animationStyle(R.style.mypopwindow_anim_style).customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                    }
                }).isWrap(true).isHaveBackGround(true).isShowBottom(true).build();
        View view1 = customPopupWindow.getContentView();
        RecyclerView popuwindow_reason_removal_RecyclerView = view1.findViewById(R.id.popuwindow_reason_removal_RecyclerView);
        TextView popuwindow_reason_removal__cancel_txt = view1.findViewById(R.id.popuwindow_reason_removal__cancel_txt);
        popuwindow_reason_removal_RecyclerView.setLayoutManager(new LinearLayoutManager(activity));
        PopuwindowReasonRemovalAdapter adapter=new PopuwindowReasonRemovalAdapter(housePayWayBeans);
        popuwindow_reason_removal_RecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                houseReasonRemovalCallBack.callBack(housePayWayBeans.get(position));
                customPopupWindow.dismiss();
            }
        });

        popuwindow_reason_removal__cancel_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customPopupWindow.dismiss();
            }
        });
        customPopupWindow.show();
    }
    /**
     * 移除一个房屋
     * @param rentId
     */
    public void  removeHouseRent(String rentId) {
        mModel.removeHouseRent(rentId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean))
                        {
                            mRootView.showMessage("删除成功");
                            mRootView.removeHouseRentSuccess();
                        }else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 下架房源
     * @param rentId
     * @param lowerShelfReason
     */
    public void  lowerShelfHouseRent(String rentId, String lowerShelfReason) {
        mModel.lowerShelfHouseRent(rentId,lowerShelfReason).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean))
                        {
                            mRootView.showMessage("下架成功");
                            mRootView.lowerShelfHouseRentSuccess();
                        }else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 根据id获取用户发布的信息
     * @param
     * @param
     */
//   public void getHouseRentByUser(String page, String userId){
//        mModel.getHouseRentByUser(String.valueOf(AppConstant.pageSize),page,userId).compose(RxUtils.applySchedulers(mRootView))
//                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HouseRentListShowInfoBean>>>(mErrorHandler) {
//                    @Override
//                    public void onNext(ResultBean<List<HouseRentListShowInfoBean>> listResultBean) {
//                        if (AppUtil.isSuccess(listResultBean))
//                        {
//                            mRootView.getMeListSuccess(listResultBean.getData());
//                        }
//                    }
//                });
//   }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
